
#include <gtkmm/enums.h>
#include <gdk/gdkrgb.h>
#include <gdkmm/drawable.h>
#include <gdkmm/gc.h>
#include <gtkmm-2.4/gtkmm/widget.h>
#include <gdkmm-2.4/gdkmm/window.h>

#include "ImageView.hpp"

namespace imp {
	namespace gui {

		ImageView::ImageView() {
			this->data = NULL;
		}

		ImageView::~ImageView() {
			stop_show();
		}

		void ImageView::show(unsigned char * data, int width, int height, ImageView::ImageType type) {

			this->data = data;
			this->width = width;
			this->height = height;
			this->type = type;

			queue_draw();
			//queue_draw_area(0, 0, width, height);
		}

		void ImageView::stop_show() {
			this->data = NULL;
		}

		bool ImageView::on_expose_event(GdkEventExpose* event) {

			if (!this->data) {
				return false;
			}

			switch (type) {
				case GRAY:
					get_window()->draw_gray_image(
							get_style()->get_fg_gc(Gtk::STATE_NORMAL),
							0, 0,
							width, height,
							Gdk::RGB_DITHER_NORMAL,
							data, width);
					break;
				case RGB:
					get_window()->draw_rgb_image(
							get_style()->get_fg_gc(Gtk::STATE_NORMAL),
							0, 0,
							width, height,
							Gdk::RGB_DITHER_NORMAL,
							data, width * 3);
					break;
			}


			return false;

		}

	}

}
